﻿Imports System.Data.OleDb

Public Class Register
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RegisterUser.ContinueDestinationPageUrl = Request.QueryString("ReturnUrl")
    End Sub

    Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, False)

        Dim continueUrl As String = RegisterUser.ContinueDestinationPageUrl
        If String.IsNullOrEmpty(continueUrl) Then
            continueUrl = "~/"
        End If

        Session("Registering") = "Yes"

        'Insert record inside Users table
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        Dim SqlString As String = "Insert into Users(UserName, Active) Values (@param1, True)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", RegisterUser.UserName)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Insert record inside aspnet_UsersInRoles with RoleId = 2 - User
        Dim createdUser As MembershipUser = Membership.GetUser(RegisterUser.UserName)
        Dim userID As Int32 = createdUser.ProviderUserKey.ToString()

        SqlString = "Insert into aspnet_UsersInRoles(UserId, RoleId) Values (@param1, 2)"
        cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", userID)
        cmd.ExecuteNonQuery()

        Response.Redirect("../User.aspx")
    End Sub
End Class